package com.bnz.manager.service.impl;

import com.bnz.manager.utils.ConnectionVo;
import com.bnz.manager.dao.SpecificationOptionDao;
import com.bnz.manager.entity.SpecificationOptionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.SpecificationDao;
import com.bnz.manager.entity.SpecificationEntity;
import com.bnz.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Autowired
    private SpecificationDao specificationDao;
    @Autowired
    private SpecificationOptionDao specificationOptionDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );
//        page.getRecords().stream().forEach(t->{
//            SpecificationOptionEntity specificationOptionEntity = specificationOptionDao.selectById(t.getId());
//        });

        return new PageUtils(page);
    }

    @Override
    public ConnectionVo info(String id) {
        SpecificationEntity one = this.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("spec_id",id);
        List<SpecificationOptionEntity> list = specificationOptionDao.selectList(queryWrapper);
        ConnectionVo<SpecificationEntity, SpecificationOptionEntity> vo = new ConnectionVo<>();
        vo.setOne(one);
        vo.setMany(list);

        return vo;
    }

    @Override
    public void saveAll(ConnectionVo<SpecificationEntity, SpecificationOptionEntity> vo) {
        SpecificationEntity specificationEntity = vo.getOne();
        this.save(specificationEntity);
        System.out.println(specificationEntity);
        vo.getMany().stream().forEach(t->{
            t.setSpecId(specificationEntity.getId());
            specificationOptionDao.insert(t);
        });
    }

    @Override
    public void updateAll(ConnectionVo<SpecificationEntity, SpecificationOptionEntity> vo) {
         specificationOptionDao.delete(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", vo.getOne().getId()));

         specificationDao.updateById(vo.getOne());

         vo.getMany().stream().forEach(t->{
             t.setSpecId(vo.getOne().getId());
             specificationOptionDao.insert(t);
         });
    }

    @Override
    public void deleteIds(String[] ids) {
        this.removeByIds(Arrays.asList(ids));
        specificationOptionDao.delete(new QueryWrapper<SpecificationOptionEntity>().in("spec_id",ids));
    }
}

